﻿using System;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using NetBay.Core.BusinessEntities;
using Rio.Applications.Web.Services;
using Rio.Framework.Pages.CriteriaControls;
using Telerik.Windows.Controls;
using Rio.Framework.Pages.ChildWindows;
using Rio.Framework.Localisation;

namespace Rio.Framework.Pages.ChildWindows
{
    public partial class VentilateWindow : RadWindow
    {
        List<long> m_oIdDocList;
        long m_lIdChrono = 0;
        long m_lUserId = 0;
        string m_sObject = string.Empty;
        List<long> m_oSenderIds;
        public string Destination
        {
            get
            {
                return ToTextBox.Text;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="VentilatePage" /> class.
        /// </summary>
        public VentilateWindow()
        {
            InitializeComponent();
            this.KeyUp += MessageBoxChildWindow_KeyUp;
            InitLabel();

            this.CanMove = true;
            this.ResizeMode = Telerik.Windows.Controls.ResizeMode.NoResize;
            this.WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterScreen;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="VentilatePage" /> class.
        /// </summary>
        /// <param name="p_oIdDocList">The p_o id doc list.</param>
        /// <param name="p_lIdChrono">The P_L id chrono.</param>
        /// <param name="p_lIdUser">The P_L id user.</param>
        /// <param name="p_sObject">The P_S object.</param>
        /// <param name="p_oSenderIds">The p_o sender ids.</param>
        public VentilateWindow(List<long> p_oIdDocList, long p_lIdChrono, long p_lIdUser, string p_sObject, List<long> p_oSenderIds = null)
        : this()
        {
            m_oIdDocList = p_oIdDocList;
            m_lIdChrono = 0; //No chrono ID in that case;
            m_lUserId = p_lIdUser;
            ObjectTextBox.Text = p_sObject;
            m_oSenderIds = p_oSenderIds;
        }

        /// <summary>
        /// Handles the KeyUp event of the MessageBoxChildWindow control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs" /> instance containing the event data.</param>
        private void MessageBoxChildWindow_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        /// <summary>
        /// Inits the label.
        /// </summary>
        private void InitLabel()
        {
            buttonSend.Content = Resx.GetLabel(Constantes.k_SEND);
            buttonCancel.Content = Resx.GetLabel(Constantes.k_CANCEL);
            ButtonTo.Content = Resx.GetLabel(Constantes.k_TO) + " :";
            LabelObject.Content = Resx.GetLabel(Constantes.k_OJECT) + " :";
            LabelMemo.Content = Resx.GetLabel(Constantes.k_MEMO) + " :";
            CheckBoxBeingNotified.Content = Resx.GetLabel(Constantes.K_BEING_NOTIFIED);
            CheckBoxNotifyReceiver.Content = Resx.GetLabel(Constantes.K_NOTIFY_RECEIVER);
            ValidatorMsg.Text = Resx.GetLabel(Constantes.K_VALIDATE_MSG);
       }

        /// <summary>
        /// Handles the Click event of the buttonLocalLoad control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void buttonLocalLoad_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog L_File = new OpenFileDialog();
            L_File.Multiselect = false;
            if (L_File.ShowDialog() == true)
            {
                //UCViewer.Load L_File.File;
            }
        }

        /// <summary>
        /// Handles the HtmlChanged event of the RadEditorComment control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Windows.RadRoutedEventArgs" /> instance containing the event data.</param>
        private void RadEditorComment_HtmlChanged(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
        }

        /// <summary>
        /// Handles the Click event of the buttonSend control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void buttonSend_Click(object sender, RoutedEventArgs e)
        {
            if (ValidateFields())
            {
                Ventilate l_oVentilate = new Ventilate(m_oIdDocList, m_lIdChrono, m_lUserId, ObjectTextBox.Text, MemoTextBox.Text, ToTextBox.Text + ";", CheckBoxBeingNotified.IsChecked.Value, CheckBoxNotifyReceiver.IsChecked.Value);
                l_oVentilate.VentilateDone += new EventHandler(l_oVentilate_VentilateDone);
                l_oVentilate.Send();
            }
            else
            {
                ValidatorMsg.Visibility = Visibility.Visible;
            }
        }

        /// <summary>
        /// Validates the fields.
        /// </summary>
        /// <returns></returns>
        private bool ValidateFields()
        {
            bool l_bValidationResult = true;

            if (string.IsNullOrEmpty(ToTextBox.Text))
            {
                l_bValidationResult = false;
            }

            if (string.IsNullOrEmpty(ObjectTextBox.Text))
            {
                l_bValidationResult = false;
            }

            return l_bValidationResult;
        }

        /// <summary>
        /// Handles the VentilateDone event of the l_oVentilate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        void l_oVentilate_VentilateDone(object sender, EventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the buttonTo control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void buttonTo_Click(object sender, RoutedEventArgs e)
        {
            GroupFilterWindow L_oGroupFilterWindow = new GroupFilterWindow(ToTextBox.Text);
            L_oGroupFilterWindow.ShowDialog();
            L_oGroupFilterWindow.Closed += new EventHandler<Telerik.Windows.Controls.WindowClosedEventArgs>(L_oGroupFilterWindow_Closed);
            L_oGroupFilterWindow.ShowDialog();
        }

        /// <summary>
        /// Group filter window event closing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void L_oGroupFilterWindow_Closed(object sender, EventArgs e)
        {
            GroupFilterWindow L_oGroupFilterWindow = (GroupFilterWindow)sender;

            //if a value has been selected
            if (L_oGroupFilterWindow.DialogResult == true)
            {
                try
                {
                    List<Group> l_oGroupList = L_oGroupFilterWindow.m_szSelectedItems;
                    if (l_oGroupList != null && l_oGroupList.Count > 0)
                    {
                        ToTextBox.Text = l_oGroupList[0].Name;
                        if (l_oGroupList.Count > 1)
                        {
                            for (int i = 1; i < l_oGroupList.Count; i++)
                            {
                                ToTextBox.Text += ";" + l_oGroupList[i].Name;
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    //Logger.Write("CANNOT GET SELECTED GROUP : " + Environment.NewLine + ex, Logger.Level.Error);
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the buttonCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs" /> instance containing the event data.</param>
        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

    }
}
